package com.aaa.service.xg.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.dao.xg.ResultFpaDao;
import com.aaa.dao.xg.TesterFpaDao;
import com.aaa.dao.xg.ViewResultDao;
import com.aaa.pojo.TesterFpa;
import com.aaa.pojo.TesterVo;
import com.aaa.pojo.ViewResultFpa;
import com.aaa.service.xg.ResultFpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class ResultFpaServiceImpl implements ResultFpaService {
    @Autowired
    private TesterFpaDao testerFpaDao;
    @Autowired
    private ResultFpaDao resultFpaDao;
    @Autowired
    private ViewResultDao viewResultDao;

    @Override
    public String saveTestResult(List<Map<String, Object>> answerList, TesterVo tester) {
        TesterFpa testerFpa = new TesterFpa();
        BeanUtil.copyProperties(tester,testerFpa,new String[]{"id"});
        testerFpaDao.insert(testerFpa);
        int testerId = testerFpa.getId();
        int i = insertBatch(answerList, testerId);
        return i>0 ? selectResult(testerId) : null ;
    }

    @Override
    public int insertBatch(List<Map<String, Object>> answerList, int testerId) {
       return resultFpaDao.insertBatch(answerList,testerId);

    }

    @Override
    public String selectResult(int testerId) {
        ViewResultFpa viewFpa = viewResultDao.findById(testerId);

        double red = viewFpa.getRedCount();
        double yellow = viewFpa.getYellowCount();
        double blue = viewFpa.getBlueCount();
        double green = viewFpa.getGreenCount();
        if (red > yellow && red > blue && red > green) {
            return "/view/red.jsp";
        } else if (yellow > red && yellow > blue && yellow > green) {
            return "/view/yellow.jsp";
        } else if (blue > red && blue > yellow && blue > green) {
            return "/view/blue.jsp";
        } else {
            return green > red && green > yellow && green > blue ? "/view/green.jsp" : "/view/All.jsp";
        }
    }
}
